// $codepro.audit.disable defineLoadFactor
package com.scribbles.user.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.scribbles.common.controller.BaseController;
import com.scribbles.common.dto.AppUserDTO;
import com.scribbles.common.entity.AppUser;
import com.scribbles.user.service.UserService;
import com.scribbles.user.util.UserHelper;

@Controller
@RequestMapping(value = "user")
public class UserController extends BaseController {

	Logger logger = Logger.getLogger(UserController.class);

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/signup", method = RequestMethod.POST)
	public Map<String, ? extends Object> signup(
			@RequestParam("record") String record) {
		logger.info("App USer : " + record);

		Gson gson = new Gson();
		AppUserDTO appUserDTO = gson.fromJson(record, AppUserDTO.class);
		logger.info(appUserDTO);

		AppUser appUser = convertAppUserDTOtoEntity(appUserDTO);
		userService.createUser(appUser);

		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", true);
		modelMap.put("message", "User " + appUserDTO.getFirstName() + " "
				+ appUserDTO.getLastName() + " created in the system");
		return modelMap;
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public Map<String, ? extends Object> save(
			@RequestParam("record") String record) {
		logger.info("App USer : " + record);

		Gson gson = new GsonBuilder().setDateFormat("dd/mm/yyyy").create();
		AppUserDTO appUserDTO = gson.fromJson(record, AppUserDTO.class);
		logger.info(appUserDTO);

		AppUser appUser = convertAppUserDTOtoEntity(appUserDTO);
		userService.updateUser(appUser);

		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", true);
		modelMap.put("message", "User " + appUserDTO.getFirstName() + " "
				+ appUserDTO.getLastName() + " created in the system");
		return modelMap;
	}

	@RequestMapping(value = "editProfile/{userId}", method = RequestMethod.GET)
	public Map<String, Object> editProfile(@PathVariable Integer userId) {
		logger.info("User ID passed : " + userId);
		// AppUser currentAppUser = UserHelper.getCurrentUser();
		Map<String, Object> modelMap = new HashMap<String, Object>();
		AppUser appUser = userService.getUser(userId);
		AppUserDTO appUserDTO = convertAppUserEntitytoDTO(appUser);
		modelMap.put("success", true);
		modelMap.put("data", appUserDTO);
		return modelMap;
	}

	private AppUser convertAppUserDTOtoEntity(AppUserDTO appUserDTO) {
		AppUser appUser = new AppUser();
		appUser.setFirstName(appUserDTO.getFirstName());
		appUser.setLastName(appUserDTO.getLastName());
		appUser.setEmailId(appUserDTO.getEmailId());
		appUser.setId(appUserDTO.getId());

		appUser.setUserId(appUserDTO.getUserId());
		appUser.setPassword(appUserDTO.getPassword());
		appUser.setSex(appUserDTO.getSex());
		appUser.setUpdatedBy(UserHelper.getCurrentUser().getId());
		return appUser;
	}

	private AppUserDTO convertAppUserEntitytoDTO(AppUser appUser) {
		AppUserDTO appUserDTO = new AppUserDTO();
		appUserDTO.setEmailId(appUser.getEmailId());
		appUserDTO.setFirstName(appUser.getFirstName());
		appUserDTO.setLastName(appUser.getLastName());
		appUserDTO.setUserId(appUser.getUserId());
		appUserDTO.setId(appUser.getId());
		appUserDTO.setDob(appUser.getDob());
		return appUserDTO;
	}
}